/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VendedoresVentana.java
 *
 * Created on 22/11/2010, 14:47:38
 */

package ar.com.educacionit.vehiculos.ventanas;

import ar.com.educacionit.vehiculos.entidades.Vendedor;
import java.util.*;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author matias
 */
public class VendedoresVentana extends javax.swing.JInternalFrame {
    DefaultListModel modeloVend= new DefaultListModel();
    private VentanaMaestra vm;
    public void limpiarCampos(){
        jTfNombre.setText("");
        jTfApellido.setText("");
        jTfDNI.setText("");
        jTfCantvend.setText("");
        jTfNombre.requestFocus();
    }

    public boolean camposCompletos(){
        if(jTfNombre.getText().equals("")){
                JOptionPane.showMessageDialog(this,
                        "El Nombre no puede estar incompleto",
                        "Mensaje",
                        JOptionPane.WARNING_MESSAGE);
                jTfNombre.requestFocus();
                return false;
        }
        if(jTfApellido.getText().equals("")){
                    JOptionPane.showMessageDialog(this,
                        "El Apellido no puede estar incompleto",
                        "Mensaje",
                        JOptionPane.WARNING_MESSAGE);
                jTfApellido.requestFocus();
        }
        if(jTfDNI.getText().equals("")){
                    JOptionPane.showMessageDialog(this,
                        "El Nro de documento no puede estar incompleto",
                        "Mensaje",
                        JOptionPane.WARNING_MESSAGE);
                jTfDNI.requestFocus();
                return false;
        }
        if(jTfCantvend.getText().equals("")){
                    JOptionPane.showMessageDialog(this,
                        "La Cant. de autos vendidos no puede estar incompleta",
                        "Mensaje",
                        JOptionPane.WARNING_MESSAGE);
                    jTfCantvend.requestFocus();
                    return false;
            }
        return true;
    }

    public boolean validarCampos(){
        try{
            int numero= Integer.parseInt(jTfCantvend.getText());
        }catch(Exception e){
            JOptionPane.showMessageDialog(this,
                    "La cantidad de autos vendidos debe ser un numero entero",
                    "ERROR",
                    JOptionPane.ERROR_MESSAGE);
            jTfCantvend.requestFocus();
            return false;
        }
        return true;
    }

    public void grabarDb(){
        boolean nuevo= true;
        JOptionPane.showMessageDialog(this,
                        "El vendedor fue guardado con éxito",
                        "Mensaje",
                        JOptionPane.INFORMATION_MESSAGE);

        Vendedor v= new Vendedor();

        if(jListVend.getSelectedIndex() != -1){
            v= (Vendedor) jListVend.getSelectedValue();
            nuevo=false;
        }
        v.setNombre(jTfNombre.getText());
        v.setApellido(jTfApellido.getText());
        v.setNumeroDocumento(jTfDNI.getText());
        v.setCantAutosVendidos(Integer.parseInt(jTfDNI.getText()));
        try {
            if (nuevo) {
                modeloVend.addElement(v);
                v.insertar(vm.getConnection());
                jListVend.clearSelection();
            } else {
                v.modificar(vm.getConnection());
            }
        } catch (Exception e) {
            
        }
        limpiarCampos();
        jButtonGuardar.setEnabled(false);
        jButtonNuevo.setEnabled(true);
    }

    public void cargarLista(){
        Vendedor v= new Vendedor();
        try{
            ArrayList vList=  v.obtenerTodos(vm.getConnection());
            Iterator itV= vList.iterator();

            while(itV.hasNext()){
                Vendedor vend= (Vendedor) itV.next();
                modeloVend.addElement(vend);
            }
        }catch(Exception e){

        }
    }
    

    public VendedoresVentana(VentanaMaestra vm) {
        this.vm=vm;
        initComponents();
        cargarLista();
        jListVend.setModel(modeloVend);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListVend = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTfNombre = new javax.swing.JTextField();
        jTfApellido = new javax.swing.JTextField();
        jTfDNI = new javax.swing.JTextField();
        jTfCantvend = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jButtonNuevo = new javax.swing.JButton();
        jButtonGuardar = new javax.swing.JButton();
        jButtonEliminar = new javax.swing.JButton();
        jButtonCerrar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Administración de Vendedores");
        setEnabled(false);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Lista de vendedores", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 1, 12))); // NOI18N

        jListVend.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jListVendMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jListVend);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jLabel1.setText("Nombre");

        jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jLabel2.setText("Apellido");

        jLabel3.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jLabel3.setText("Nro Documento");

        jLabel4.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jLabel4.setText("Autos Vendidos");

        jButtonNuevo.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jButtonNuevo.setText("Nuevo");
        jButtonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNuevoActionPerformed(evt);
            }
        });

        jButtonGuardar.setFont(new java.awt.Font("Ubuntu", 1, 13)); // NOI18N
        jButtonGuardar.setText("Guardar");
        jButtonGuardar.setEnabled(false);
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jButtonEliminar.setFont(new java.awt.Font("Ubuntu", 1, 13)); // NOI18N
        jButtonEliminar.setText("Eliminar");
        jButtonEliminar.setEnabled(false);
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });

        jButtonCerrar.setFont(new java.awt.Font("Ubuntu", 1, 13));
        jButtonCerrar.setText("Cerrar");
        jButtonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTfCantvend, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                            .addComponent(jTfNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                            .addComponent(jTfApellido, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                            .addComponent(jTfDNI, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 163, Short.MAX_VALUE)
                        .addComponent(jButtonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTfApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTfDNI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTfCantvend, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonNuevo)
                    .addComponent(jButtonGuardar)
                    .addComponent(jButtonCerrar)
                    .addComponent(jButtonEliminar))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed
        vm.getjMenuAbrirVend().setEnabled(true);
    }//GEN-LAST:event_formInternalFrameClosed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        vm.getjMenuAbrirVend().setEnabled(false);
    }//GEN-LAST:event_formInternalFrameOpened

    private void jButtonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButtonCerrarActionPerformed

    private void jButtonNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNuevoActionPerformed
        limpiarCampos();
        jButtonNuevo.setEnabled(false);
        jButtonGuardar.setEnabled(true);
    }//GEN-LAST:event_jButtonNuevoActionPerformed

    private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed
        if(camposCompletos()){
            if(validarCampos()){
               grabarDb();
               jListVend.updateUI();
            }
        }
    }//GEN-LAST:event_jButtonGuardarActionPerformed

    private void jListVendMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListVendMouseClicked
        jButtonGuardar.setEnabled(true);
        jButtonEliminar.setEnabled(true);

        Vendedor v= (Vendedor) jListVend.getSelectedValue();
        jTfNombre.setText(v.getNombre());
        jTfApellido.setText(v.getApellido());
        jTfDNI.setText(v.getNumeroDocumento());
        jTfCantvend.setText(String.valueOf(v.getCantAutosVendidos()));

    }//GEN-LAST:event_jListVendMouseClicked

    private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed
        try {
            int rta = JOptionPane.showConfirmDialog(this, "Desea eliminar el vendedor " + jListVend.getSelectedValue() + "?", "Eliminar", JOptionPane.YES_NO_OPTION);
            if (rta == JOptionPane.NO_OPTION) {
                return;
            }
            Vendedor v = (Vendedor) jListVend.getSelectedValue();
            modeloVend.remove(jListVend.getSelectedIndex());
            v.eliminar(vm.getConnection());
            jButtonGuardar.setEnabled(false);
            jButtonEliminar.setEnabled(false);
            jButtonNuevo.setEnabled(true);
            limpiarCampos();
        } catch (Exception ex) {
            
        }
    }//GEN-LAST:event_jButtonEliminarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCerrar;
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JButton jButtonNuevo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jListVend;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTfApellido;
    private javax.swing.JTextField jTfCantvend;
    private javax.swing.JTextField jTfDNI;
    private javax.swing.JTextField jTfNombre;
    // End of variables declaration//GEN-END:variables

}
